package com.sanploy.fed.push.controller;

import com.google.gson.Gson;
import com.sanploy.fed.push.bean.GovNews;
import com.sanploy.fed.push.bean.NewsClass;
import com.sanploy.fed.push.config.SystemConfig;
import com.sanploy.fed.push.resp.BaseListResp;
import com.sanploy.fed.push.resp.BaseRspe;
import com.sanploy.fed.push.resp.PushBaseBeanResp;
import com.sanploy.fed.push.service.FileService;
import com.sanploy.fed.push.service.NewsService;
import com.sanploy.fed.push.util.RespUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/16.
 */
@RestController
public class NewsController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private FileService fileService;


    private Gson gson = new Gson();

    //上传新闻
    @RequestMapping(value = "news", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String updateNews(ServletRequest servletRequest) {
        try {
            String title = servletRequest.getParameter("title");
            String introduce = servletRequest.getParameter("introduce");
            String content = servletRequest.getParameter("editor");
            String author = servletRequest.getParameter("author");
            String filetype = servletRequest.getParameter("filetype");
            String fileName = "";
            String newsclass = servletRequest.getParameter("secondclass");
            String classname = servletRequest.getParameter("classname");
            if (servletRequest instanceof MultipartHttpServletRequest) {
                Map<String, MultipartFile> fileMap = ((MultipartHttpServletRequest) servletRequest).getFileMap();

                if (fileMap.size() ==0){
                 return    SystemConfig.Page_Fail;
                }

                for (Map.Entry<String, MultipartFile> map : fileMap.entrySet()) {
                    fileName = fileService.savePhoto(map.getValue(), "", "","newsphoto",filetype);
                }
            }
            newsService.addNews(title, content, author, fileName, newsclass, introduce,classname);
            return SystemConfig.Page_Successful;
        } catch (Exception e) {
            return SystemConfig.Page_Fail;
        }

    }

    //上传新闻图片
    @RequestMapping(value = "news/photo", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String updateNewsPhoto(ServletRequest servletRequest, HttpServletResponse response) {
        String resp;
        try {

            String callback = servletRequest.getParameter("CKEditorFuncNum");
            String fileName = "";
            if (servletRequest instanceof MultipartHttpServletRequest) {
                Map<String, MultipartFile> fileMap = ((MultipartHttpServletRequest) servletRequest).getFileMap();
                String.valueOf(fileMap.size());
                for (Map.Entry<String, MultipartFile> map : fileMap.entrySet()) {
                    fileName = fileService.savePhoto(map.getValue(), "", "","newsphoto","no");
                }
            }
            resp = "<script type=\"text/javascript\">" +
                    "window.parent.CKEDITOR.tools.callFunction(" + callback
                    + ",'" + fileName + "','')</script>";

        } catch (Exception e) {
            resp = "失败";
        } finally {
            response.setHeader("X-Frame-Options", "AllowAll");
        }

        return resp;
    }

    //查看某条新闻
    @RequestMapping(value = "news", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String findOneNews(@RequestParam Long id) {
        PushBaseBeanResp<GovNews> baseResp = new PushBaseBeanResp<GovNews>();
        String resp;
        try {
            baseResp.setBean(newsService.getNewsById(id));
            baseResp.setType("news");
    RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }

        return resp;
    }


    //搜索新闻
    @RequestMapping(value = "news/search", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String findOneNews(@RequestParam String key) {
        BaseListResp<GovNews> baseResp = new BaseListResp<GovNews>();
        String resp;
        try {
            baseResp.setList(newsService.searchNews(key));
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //分页查看新闻
    @RequestMapping(value = "news/page", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String findPageNews(@RequestParam int count, @RequestParam int page) {
        BaseListResp<GovNews> baseResp = new BaseListResp<GovNews>();
        String resp;
        try {
            baseResp.setList(newsService.getNewsByPage(count, page));
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }
    //分页查看所有新闻
    @RequestMapping(value = "news/all", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String findPageNewsAll(@RequestParam int count, @RequestParam int page) {
        BaseListResp<GovNews> baseResp = new BaseListResp<GovNews>();
        String resp;
        try {
            baseResp.setList(newsService.getNewsByPageAll(count, page));
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }



    //查看某个分类下的特定条数的新闻
    @RequestMapping(value = "news/page/class", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String getNewsByclass(@RequestParam Long newsClass,@RequestParam int count,@RequestParam int page) {
        BaseListResp<GovNews> baseResp = new BaseListResp<GovNews>();
        String resp;
        try {
            baseResp.setList(newsService.getNewsByClassLimit(newsClass, count, page));
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //查看某个分类下所有新闻条数
    @RequestMapping(value = "news/class/count", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String getNewsByclass(@RequestParam Long newsClass) {
        BaseRspe baseResp = new BaseRspe();
        String resp;
        try {
            RespUtil.baserespSuccessful(baseResp);
            baseResp.setMsg(String.valueOf(newsService.getNewsNumber(newsClass)));
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //获取新闻类别
    @RequestMapping(value = "news/class", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String setClassname(ServletRequest servletRequest) {
        BaseListResp<NewsClass> baseResp = new BaseListResp<NewsClass>();
        String resp;
        try {
            baseResp.setList(newsService.findAllNewClass());
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //获取某分类下新闻类别
    @RequestMapping(value = "news/class/parent", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String getclassByParent(@RequestParam Long parentId) {
        BaseListResp<NewsClass> baseResp = new BaseListResp<NewsClass>();
        String resp;
        try {
            baseResp.setList(newsService.getClassByParent(parentId));
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //查看一级分类下的所有新闻
    @RequestMapping(value = "news/class/one", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String getclassOne(@RequestParam Long id,@RequestParam int count,@RequestParam int page) {
        BaseListResp<GovNews> baseResp = new BaseListResp<GovNews>();
        String resp;
        try {
            baseResp.setList(newsService.getclassOneNew(id,count,page));
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //查看一级分类下的所有新闻条数
    @RequestMapping(value = "news/class/one/count", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String getclassOne(@RequestParam Long id) {
        BaseRspe baseResp = new BaseRspe();
        String resp;
        try {
            RespUtil.baserespSuccessful(baseResp);
            baseResp.setMsg(String.valueOf(newsService.getOneNewsNumber(id)));
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

    //增加分类新闻类别
    @RequestMapping(value = "news/class", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String findNewsClass(String classname,@RequestParam(defaultValue = "0") Long parent) {
        BaseRspe baseResp = new BaseRspe();
        try {
            newsService.addNewsClass(classname,parent);
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        }
        return gson.toJson(baseResp);
    }

    //删除新闻类别
    @RequestMapping(value = "news/class", method = RequestMethod.DELETE, produces = "text/html;charset=utf-8")
    public String deleteNewsClass(@RequestParam Long id) {
        BaseRspe baseResp = new BaseRspe();
        try {
            newsService.deleteNewsClass(id);
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        }
        return gson.toJson(baseResp);
    }

    //修改新闻类别
    @RequestMapping(value = "news/class", method = RequestMethod.PUT, produces = "text/html;charset=utf-8")
    public String putNewsClass(@RequestParam String name,@RequestParam Long id) {
        BaseRspe baseResp = new BaseRspe();
        try {
            newsService.updateNewsClass(name,id);
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        }
        return gson.toJson(baseResp);
    }


    //查看有多少条新闻新闻
    @RequestMapping(value = "news/number", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String findPageNews() {
        BaseRspe baseResp = new BaseRspe();
        String resp;
        try {
            RespUtil.baserespSuccessful(baseResp);
            baseResp.setMsg(String.valueOf(newsService.getNewsNumber()));
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }
    //TODO 删除了后推送的历史记录怎么办
    @RequestMapping(value = "news",method = RequestMethod.DELETE,produces = "application/json;charset=utf-8")
    public String deleteNews(@RequestParam Long id){
        BaseRspe baseResp = new BaseRspe();
        String resp;
        try {
            newsService.deleteNews(id);
            RespUtil.baserespSuccessful(baseResp);
        } catch (Exception e) {
            RespUtil.baserespFail(baseResp);
        } finally {
            resp = gson.toJson(baseResp);
        }
        return resp;
    }

}
